<?php
declare(strict_types=1);

namespace Modules\Salesmanagement\Http\Controllers;

use Catch\Base\CatchController as Controller;
use Modules\Salesmanagement\Models\Salesout;
use Modules\Salesmanagement\Models\SalesoutAttached;
use Illuminate\Http\Request;
use Modules\Salesmanagement\Http\Repositories\SalesmanagementRequest;
use Illuminate\Support\Facades\DB;
use Modules\Warehouse\Models\Storagelocation;
use Modules\Inventory\Models\Inventory;
use Catch\Exceptions\FailedException;
class SalesmanagementController extends Controller
{
    public function __construct(
        protected readonly Salesout $model
    ){}

    /**
     * @param Request $request
     * @return mixed
     */
    public function index(): mixed
{

    return $this->model->getList();
}

    /**
     * @param Request $request
     * @return mixed
     */
    public function store(Request $request)
{

    $data = $request->all();
    $res = $this->model->where('fbillno',$data['fbillno'])->first();

    if(!empty($res)){
        return throw new FailedException('单据编码已存在无法录入请联系技术处理');
        }else{
        return SalesmanagementRequest::create($data);
    }


    //return $this->model->storeBy($request->all());
}

    /**
     * @param $id
     * @return mixed
     */
    public function show($id)
{
    return $this->model->firstBy($id);
}

    /**
     * @param Request $request
     * @param $id
     * @return mixed
     */
    public function update($fid, Request $request)
{



    return SalesmanagementRequest::createUpdate($fid,$request->all());
    // return $this->model->updateBy($fid, $request->all());
}

    /**
     * @param $id
     * @return mixed
     */
    public function destroy($id)
{

    return SalesmanagementRequest::delete($fid);
    // return $this->model->deleteBy($id);
}

     /**
      * @param $id
      * @return mixed
      */
    public function Salesinterface(Request $request)
{

    $data = SalesmanagementRequest::list($request->all());
    // $data=$this->model->getList();
    foreach($data as $k=>$v){
        $SalesoutAttached = SalesoutAttached::query()
            ->select(DB::raw('SUM(fqty) as fqty'))
            ->where('salesout_id',$v['id'])
            ->groupby(['salesout_id'])
            ->first();
        if(!empty($SalesoutAttached)){
            $data[$k]['fqty'] = $SalesoutAttached->fqty;
        }else{
            $data[$k]['fqty'] = 0;
        }
        if ($v['incomingtype']==1) {
            $data[$k]['incomingtype'] = '组织间销售出库';
        }else{
            $data[$k]['incomingtype'] = '组织外销售出库';
        }

        if ($v['writestate']==1) {
            $data[$k]['writestate'] = '回写成功';
        }else{
            $data[$k]['writestate'] = '';
        }

    }

    return $data;
}


    /**PDA列表详情接口
     * @param $id
     * @return mixed
     */
    public function Salesdetails($id)
{
    return SalesmanagementRequest::SalesdetailsData($id);
}

    /*
    * 数量和金蝶推送接口 -> 采购入库单生成
    * PAD数据添加
    * @param $id
    * @return mixed
     */
    public function Pdaadddata(Request $request)
{
    $data = $request->all();
    $data['creator_id'] = $this->getLoginUserId();//当前登录用户名称
    return SalesmanagementRequest::pdassanningaddData($data);
}

    /*PDA
     *销售出库箱号扫描
     * @param $id
     * @return mixed
     */
    public function Boxnumber($code,Request $request)
{
    //return throw new FailedException('亲，技术在维修还不能出库');
    $data = $request->all();

    $inventory = Inventory::select('org_code','location','material_no','unit','lot_num','qty','cargo_code','assign_qty','lock_qty')
        ->where('lot_num',$code)
        ->get()
        ->toArray();

    if(empty($inventory)){
        throw new \Exception('该箱号无库存');
    }
    $datares = [];
    foreach($inventory as $k=>$v){

        $Storagelocation = Storagelocation::query()->with('wms')->select('*')
            ->where('warehousecode',$v['location'])
            ->first();
        if(empty($Storagelocation)){
            throw new \Exception("库存库位不存在[{$v['location']}]");
        }
        $SalesoutAttached = SalesoutAttached::query()->select(
            'salesout.id as salesout_id',
            'salesout.fid as salesout_fid',
            'salesout.fbillno',
            'salesout_attached.id as salesout_attached_id',
            'salesout_attached.fid as salesout_attached_fid',
            'salesout_attached.fseq as fseq',
            'salesout_attached.fentryid as fentryid',
            'salesout_attached.fmaterialid as fmaterialid',
            'salesout_attached.fmaterialname as fmaterialname',
            'salesout_attached.fmaterialmodel as fmaterialmodel',
            'salesout_attached.fmodel as fmodel',
            'salesout_attached.funitid_number as funitid_number',
            'salesout_attached.funitid_name as funitid_name',
            'salesout_attached.fstockid_number as fstockid_number',
            'salesout_attached.fstockid_name as fstockid_name',
            'salesout_attached.fqty as fqty',
            'salesout_attached.fownertypeid as fownertypeid',
            'salesout_attached.fownerid_number as fownerid_number',
            'salesout_attached.fownerid_name as fownerid_name',
                )
            ->join('salesout as salesout', 'salesout.id', '=', 'salesout_attached.salesout_id')
            ->where('salesout.fsaleorgid_number',$v['org_code'])//销售组织
            ->where('salesout_attached.salesout_id',$data['id'])
            ->where('salesout_attached.fstockid_number',$Storagelocation->wms->fnumber)//仓库
            //->where('salesout_attached.fstockid_name',$Storagelocation->wms->fname)//仓库名称
            ->where('salesout_attached.fmaterialid',$v['material_no'])//SKU
            ->where('salesout_attached.funitid_number',$v['unit'])//单位编码
            ->first();
        if(empty($SalesoutAttached)){
            continue;
            //return ['code'=>10005,'message'=>'无法匹配数据'];
        }

        $faty = SalesoutAttached::select(DB::raw('SUM(fqty) as fqty'))
        ->where('salesout_id',$data['id'])
        ->where('fmaterialid',$SalesoutAttached['fmaterialid'])
        ->where('fmaterialname',$SalesoutAttached['fmaterialname'])
        ->where('fmaterialmodel',$SalesoutAttached['fmaterialmodel'])
        ->where('fmodel',$SalesoutAttached['fmodel'])
        ->where('funitid_number',$SalesoutAttached['funitid_number'])
        ->where('funitid_name',$SalesoutAttached['funitid_name'])
        ->where('fownertypeid',$SalesoutAttached['fownertypeid'])
        ->where('fownerid_number',$SalesoutAttached['fownerid_number'])
        ->where('fownerid_name',$SalesoutAttached['fownerid_name'])
        ->groupby(['salesout_id','fmaterialid','fmaterialname','fmaterialmodel','fmodel','funitid_number','funitid_name','fownertypeid','fownerid_number','fownerid_name'])->first();
        // $SalesoutAttached['warehousecode'] = $Storagelocation->warehousecode;
        // $SalesoutAttached['repositoryname'] = $Storagelocation->repositoryname;
        // $SalesoutAttached['barcode'] = $v['lot_num'];
        // $SalesoutAttached['qty'] = $v['qty'];

        $datares[$k]['warehousecode'] =  $Storagelocation->warehousecode;
        $datares[$k]['repositoryname'] =  $Storagelocation->repositoryname;
        $datares[$k]['barcode'] =  $v['lot_num'];
        $datares[$k]['qty'] =  $v['qty'] - $v['assign_qty'] - $v['lock_qty'];
        $datares[$k]['salesout_id'] =  $SalesoutAttached->salesout_id;
        $datares[$k]['salesout_fid'] =  $SalesoutAttached->salesout_fid;
        $datares[$k]['fbillno'] =  $SalesoutAttached->fbillno;
        $datares[$k]['salesout_attached_id'] =  $SalesoutAttached->salesout_attached_id;
        $datares[$k]['salesout_attached_fid'] =  $SalesoutAttached->salesout_attached_fid;
        $datares[$k]['fseq'] =  $SalesoutAttached->fseq;
        $datares[$k]['fentryid'] =  $SalesoutAttached->fentryid;
        $datares[$k]['fmaterialid'] =  $SalesoutAttached->fmaterialid;
        $datares[$k]['fmaterialname'] = $SalesoutAttached->fmaterialname;
        $datares[$k]['fmaterialmodel'] = $SalesoutAttached->fmaterialmodel;
        $datares[$k]['fmodel'] = $SalesoutAttached->fmodel;
        $datares[$k]['funitid_number'] =  $SalesoutAttached->funitid_number;
        $datares[$k]['funitid_name'] = $SalesoutAttached->funitid_name;
        $datares[$k]['fstockid_number'] = $SalesoutAttached->fstockid_number;
        $datares[$k]['fstockid_name'] =  $SalesoutAttached->fstockid_name;
        $datares[$k]['fqty'] =  $faty['fqty'];
    }
    if(empty($datares)){
        throw new \Exception('该箱号库存信息与出库明细不匹配');
    }

    return $datares;

}



    /*
    * wsm数量修改
    * @param $id
    * @return mixed
     */
    public function Modifydata($id,Request $request)
{

    return SalesmanagementRequest::Modify($id,$request->all());
}

    /*
    * wsm详情接口
    * @param $id
    * @return mixed
     */
    public function Detailinterface($id,Request $request)
{

    return SalesmanagementRequest::DetailinterfaceData($id,$request->all());
}


    /*
    * 审核接口和推送接口
    * @param $id
    * @return mixed
    */
    public function Pushnotification($id,Request $request)
{

    return SalesmanagementRequest::Kingdeeaudit($id,$request->all());
}






}
